package com.clas.liu.myapplication.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.clas.liu.myapplication.R;
import com.clas.liu.myapplication.model.entity.reportedUser.ReportedUser;
import com.clas.liu.myapplication.view.activity.impl.ImageViewActivity;
import com.clas.liu.myapplication.view.activity.impl.InfoActivity;
import com.clas.liu.myapplication.view.activity.impl.ReportedUserActivity;
import com.clas.liu.myapplication.view.activity.impl.UserInfoActivity;
import com.clas.liu.myapplication.view.util.DateUtil;
import com.clas.liu.myapplication.view.util.NetworkHelper;
import com.clas.liu.myapplication.view.widget.CircleImageView;
import com.squareup.picasso.Picasso;

import java.sql.Date;
import java.util.List;

/**
 * Created by liu on 2019/5/26.
 */

public class ReportedUserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_HEADER = -1;
    private List<ReportedUser> data;
    private Context context;
    private boolean isDelete = false;
    private PopupWindow window;
    private View popupView;
    private LinearLayout ban;
    private LinearLayout cancel;
    private int clickPosition;

    public ReportedUserAdapter(Context context, List<ReportedUser> infos) {
        this.context = context;
        this.data = infos;
        initPopupWindow();

    }

    private void initPopupWindow() {
        popupView = LayoutInflater.from(context).inflate(R.layout.popup_window_more2,null);
        ban = (LinearLayout) popupView.findViewById(R.id.ban);
        cancel = (LinearLayout) popupView.findViewById(R.id.cancel);
        window = new PopupWindow(popupView,dip2px(context,80),dip2px(context,100));
//        window.setAnimationStyle(R.style.community_popup_window_anim);
//		window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
        window.setFocusable(true);
        //设置可以触摸弹出框以外的区域
        window.setOutsideTouchable(true);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    private int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case TYPE_ITEM:
                holder = new MyHolder(LayoutInflater.from(context).
                        inflate(R.layout.item_reported_user_layout,parent,false));
                break;
            case TYPE_FOOTER:
                holder = new FooterHolder(LayoutInflater.from(context).
                        inflate(R.layout.item_footer_view,parent,false));
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder1, int position) {
        switch (getItemViewType(position)) {
            case TYPE_HEADER:
                break;
            case TYPE_FOOTER:
                FooterHolder holder = (FooterHolder) holder1;
                holder.foot_view_item_tv.setText("正在加载...");
                if (isDelete) {
                    holder.progressBar.setVisibility(View.GONE);
                    holder.foot_view_item_tv.setText("没有更多了哦~");
                    holder.foot_view_item_tv.setAlpha(0.5f);
                    holder.foot_view_item_tv.setTextColor(context.getResources().getColor(R.color.black));
                }
                if (!NetworkHelper.isNetworkAvailable(context) && data.size() == 0) {
                    holder.progressBar.setVisibility(View.GONE);
                    holder.foot_view_item_tv.setVisibility(View.VISIBLE);
                    holder.loadLayout.setVisibility(View.GONE);
                } else {
                    holder.foot_view_item_tv.setVisibility(View.VISIBLE);
                    holder.loadLayout.setVisibility(View.VISIBLE);
                }
                break;
            case TYPE_ITEM:
                final MyHolder holder2 = (MyHolder) holder1;
                final ReportedUser info = data.get(position);
                holder2.name.setText(info.getName());
                Picasso.with(context).load(info.getHead()).placeholder(R.mipmap.default_head).
                        error(R.mipmap.loading).into(holder2.head);
                Date date = new Date(Long.parseLong(info.getRelease_time()));
                holder2.time.setText(DateUtil.getFormatTime(date));
                holder2.text.setText(info.getText());
                if (info.getPic() != null && !info.getPic().equals("") && !info.getPic().equals("null")) {
                    holder2.pic.setVisibility(View.VISIBLE);
                } else holder2.pic.setVisibility(View.GONE);

                holder2.pic.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, ImageViewActivity.class);
                        intent.putExtra("url", info.getPic());
                        context.startActivity(intent);
                    }
                });
                holder2.head.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, UserInfoActivity.class);
                        intent.putExtra("user_id", info.getUser_id());
                        context.startActivity(intent);
                    }
                });
                final int i = position;
                holder2.more.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.showAsDropDown(holder2.more,dip2px(context, 20),0);
                        ban.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                setClickPosition(i);
                                ((ReportedUserActivity)context).banUser(info.getUser_id(),info.getId());
                                window.dismiss();
                            }
                        });
                        cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                setClickPosition(i);
                                ((ReportedUserActivity)context).cancel(info.getId());
                                window.dismiss();
                            }
                        });
                    }
                });
                break;
        }
    }

    public void delete(){
        int position = getClickPosition();
        data.remove(position);
        this.notifyItemRemoved(position);
        if (position != data.size()) {
            this.notifyItemRangeChanged(position, data.size() - position);
        }
    }

    public void setClickPosition(int position){
        this.clickPosition = position;
    }

    public int getClickPosition(){
        return clickPosition;
    }

    public void deleteLoadingMore(boolean isDelete) {
        this.isDelete = isDelete;
        notifyItemChanged(getItemCount() - 1);
    }

    public void update(List<ReportedUser> root) {
        data = root;
        this.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return data.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else
            return TYPE_ITEM;

    }

    public class MyHolder extends RecyclerView.ViewHolder{

        CircleImageView head;
        TextView name;
        TextView time;
        TextView text;
        TextView pic;
        LinearLayout item;
        ImageView more;
        public MyHolder(View itemView) {
            super(itemView);
            head = itemView.findViewById(R.id.head_iv);
            name = itemView.findViewById(R.id.name);
            time = itemView.findViewById(R.id.time);
            text = itemView.findViewById(R.id.text);
            pic = itemView.findViewById(R.id.pic);
            item = itemView.findViewById(R.id.item);
            more = itemView.findViewById(R.id.more);
        }
    }
    public class FooterHolder extends RecyclerView.ViewHolder{
        private TextView foot_view_item_tv;
        private LinearLayout loadLayout;
        private ProgressBar progressBar;

        public FooterHolder(View view) {
            super(view);
            foot_view_item_tv = (TextView) view.findViewById(R.id.tv);
            loadLayout = (LinearLayout)view.findViewById(R.id.loadmore);
            progressBar = (ProgressBar)view.findViewById(R.id.progress);
        }
    }
}
